/* findkey.c - The routine that finds a key entry in the file. */

/*  GNU DBM  - DataBase Manager (database subroutines) by Philip A. Nelson
    Copyright (C) 1989  Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department
                Western Washington University
                Bellingham, WA 98226
        phone:  (206) 676-3035

*************************************************************************/

#include "gdbm.h"
#include "extern.h"
#include "gdbmutils.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Find the KEY in the file and get ready to read the associated data.  The
   return value is the location in the current hash bucket of the KEY's
   entry.  If it is not found, the value -1 is returned. */

int _dbm_findkey(dbm_file_info * dbf, datum key)
{
	int new_hash_val;	/* The new hash value. */
	int bucket_hash_val;	/* The hash value from the bucket. */
	char *file_key;		/* The complete key as stored in the file. */
	int elem_loc;		/* The location in the bucket. */
	int home_loc;		/* The home location in the bucket. */
	int num_bytes;		/* Number of bytes read. */
	int key_size;		/* Size of the key on the file.  */

	/* Compute hash value and load proper bucket.  */
	new_hash_val = _dbm_hash(key);
	if (!_dbm_get_bucket(dbf, new_hash_val))
		return -1;

	/* Search for element in bucket. */
	elem_loc = new_hash_val % dbf->header.bucket_elems;
	home_loc = elem_loc;
	bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
	while (bucket_hash_val != -1)
	{
		key_size = dbf->bucket->h_table[elem_loc].key_size;
		if (bucket_hash_val != new_hash_val
		    || key_size != key.dsize
		|| memcmp(dbf->bucket->h_table[elem_loc].key_start, key.dptr,
			  (SMALL < key_size ? SMALL : key_size)) != 0)
		{
			/* Current elem_loc is not the item, go to next item. */
			elem_loc = (elem_loc + 1) % dbf->header.bucket_elems;
			if (elem_loc == home_loc)
				return -1;
			bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
		}
		else
		{
			/*
			 * This may be the one we want.  The only way to tell
			 * is to read it.
			 */
			file_key = (char *) malloc(key_size);
			num_bytes = fseek(dbf->desc,
					  dbf->bucket->h_table[elem_loc].data_pointer, SEEK_SET);
			if (num_bytes != 0)
				_dbm_fatal(dbf, "seek error");
			num_bytes = fread(file_key, 1, key_size, dbf->desc);
			if (num_bytes != key_size)
				_dbm_fatal(dbf, "read error");
			if (memcmp(file_key, key.dptr, key_size) == 0)
			{
				/* This is the item. */
				free(file_key);
				return elem_loc;
			}
			else
			{
				/*
				 * Not the item, try the next one.  Return if
				 * not found.
				 */
				free(file_key);
				elem_loc = (elem_loc + 1) % dbf->header.bucket_elems;
				if (elem_loc == home_loc)
					return -1;
				bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
			}
		}
	}

	/* If we get here, we never found the key. */
	return -1;

}
